﻿using System.Threading.Tasks;
using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.Admin;
using Repository.WXProjectAdmin;
using Xproject.Util;
using Model.WXshopBuiness;
using System;
using System.Collections.Generic;
using Model.Admin.DTO;
using System.Linq.Expressions;
using System.Linq;

namespace Services.WXProjectAdmin
{
   public  class AdminUserInfoServices :Service
    {
        IRepositoryAsync<AdminUser> m_AdminUserRepository;
        IRepositoryAsync<AdminUserRole> m_AdminUserRoleRepository;
        IRepositoryAsync<AdminRoleMenu> m_AdminRoleMenuRepository;
        SystemMenuServices m_SystemMenuServices;
        public AdminUserInfoServices(IDataContextAsync dataContext,SystemMenuServices mSystemMenuServices) : base(dataContext)
        {
            m_AdminUserRepository = m_UnitOfWork.RepositoryAsync<AdminUser>();
            m_AdminUserRoleRepository = m_UnitOfWork.RepositoryAsync<AdminUserRole>();
            m_AdminRoleMenuRepository = m_UnitOfWork.RepositoryAsync<AdminRoleMenu>();
            m_SystemMenuServices = mSystemMenuServices;
        }
        public bool AddAdminUser(AdminUser user, string userId)
        {
            if (user != null)
            {
                user.ID = Guid.NewGuid().ToString();
                user.EntityState = EntityState.Added;
                user.CreateTime = DateTime.Now;
                user.UpdateTime = DateTime.Now;
                user.Creator = user.Updator = userId;
                m_AdminUserRepository.Insert(user);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }
        /// <summary>
        /// 删除最好不做硬删除  做软删除
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public bool DelAdminUser(AdminUser user)
        {
            if (user != null)
            {
                user.EntityState = EntityState.Deleted;
                m_AdminUserRepository.Delete(user);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }
        public bool EditAdminUser(AdminUser user, string userId)
        {
            if (user != null)
            {
                user.EntityState = EntityState.Modified;
                user.UpdateTime = DateTime.Now;
                user.Updator = userId.ToString();
                m_AdminUserRepository.Update(user);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }

        public AdminUser GetById(string Id)
        {
            return m_AdminUserRepository.Get(Id);
        }

        public AdminUser GetUserInfoByPwd(string userName, string password)
        {
          return  m_AdminUserRepository.GetByPassword(userName, password);
        }

        public List<AdminUser> GetAllAdminUser(string site, int offset, int pageSize, string userName, int? userCode, string mobile, string email,
            DateTime? startTime, DateTime? endTime, string order, string ordername, out int total)
        {
            total = 0;
            List<AdminUser> result = m_AdminUserRepository.GetAllAdminUser(site, userName, userCode, mobile, email, startTime, endTime, order, ordername);
            total = result.Count;
            return result.Skip(offset).Take(pageSize).ToList();
        }

        public List<AdminUserRole> GetUserRoles(int userId)
        {
            return m_AdminUserRoleRepository.GetUserRoles(userId);
        }

        public List<AdminRoleMenu> GetRoleMenus(List<int> roleID)
        {
            return m_AdminRoleMenuRepository.GetRoleMenus(roleID);
        }
        /// <summary>
        /// 得到用户菜单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<AdminMenu> GetUserMenus(int userId,string site)
        {
            //先得到用户的角色
            List<AdminUserRole> userroles = m_AdminUserRoleRepository.GetUserRoles(userId);
            if (userroles != null && userroles.Count > 0)
            {
                List<int> roleids = userroles.Select(i => i.RoleID).Distinct().ToList();
                if (roleids != null && roleids.Count > 0)
                {
                    List<AdminRoleMenu> rolemenuids = GetRoleMenus(roleids);
                    if (rolemenuids != null && rolemenuids.Count > 0)
                    {
                        List<int> menuids = rolemenuids.Select(i => i.MenuId).Distinct().ToList();
                        if (menuids != null && menuids.Count > 0)
                        {
                             List<AdminMenu> adminmenus = m_SystemMenuServices.GetAdminMenuList(site);
                            return adminmenus.Where(i => menuids.Contains(i.MenuId)).ToList();
                        }
                    }
                }
            }
            return null;
        }


    }
}
